﻿<?php
	// API Key
	define("KEY", "gi6mmn5fmidijlc9thyhokl8z0fdtby9t4wgs2wek9");
	header("Content-Type: text/html; charset=utf-8");

	// API Target
	$url = "http://api-pub.dictionary.com/v001";

	// Receive parameters sent from HTML page
	if (isset($_GET["q"])) {
		$query = trim($_GET["q"]);
	}
	$encodedquery = urlencode($query);
	if (isset($_GET["type"])) {
		$type = trim($_GET["type"]);
	}
	
	$urlWithParam = $url."?vid=".KEY."&q=".$encodedquery."&type=".$type;

	$data = file_get_contents($urlWithParam);

function object2array($object)
{
    $return = NULL;
        
    if(is_array($object))
    {
        foreach($object as $key => $value)
            $return[$key] = object2array($value);
    }
    else
    {
        $var = get_object_vars($object);
            
        if($var)
        {
            foreach($var as $key => $value)
                $return[$key] = object2array($value);
        }
        else
            return $object;
    }

    return $return;
}

	function simpleXMLToArray($xml, 
                    $flattenValues=true,
                    $flattenAttributes = true,
                    $flattenChildren=true,
                    $valueKey='@value',
                    $attributesKey='@attributes',
                    $childrenKey='@children'){

        $return = array();
        if(!($xml instanceof SimpleXMLElement)){return $return;}
        $name = $xml->getName();
        $_value = trim((string)$xml);
        if(strlen($_value)==0){$_value = null;};

        if($_value!==null){
            if(!$flattenValues){$return[$valueKey] = $_value;}
            else{$return = $_value;}
        }

        $children = array();
        $first = true;
        foreach($xml->children() as $elementName => $child){
            $value = simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
            if(isset($children[$elementName])){
                if($first){
                    $temp = $children[$elementName];
                    unset($children[$elementName]);
                    $children[$elementName][] = $temp;
                    $first=false;
                }
                $children[$elementName][] = $value;
            }
            else{
                $children[$elementName] = $value;
            }
        }
        if(count($children)>0){
            if(!$flattenChildren){$return[$childrenKey] = $children;}
            else{$return = array_merge($return,$children);}
        }

        $attributes = array();
        foreach($xml->attributes() as $name=>$value){
            $attributes[$name] = trim($value);
        }
        if(count($attributes)>0){
            if(!$flattenAttributes){$return[$attributesKey] = $attributes;}
            else{$return = array_merge($return, $attributes);}
        }
        
        return $return;
    }
	
	 function simplexml_unCDATAise($xml) {
		$new_xml = NULL;
		preg_match_all("/\<\!\[CDATA \[(.*)\]\]\>/U", $xml, $args);

		if (is_array($args)) {
			if (isset($args[0]) && isset($args[1])) {
				$new_xml = $xml;
				
				for ($i=0; $i<count($args[0]); $i++) {
					$old_text = $args[0][$i];
					$new_text = htmlspecialchars($args[1][$i]);
					$new_xml = str_replace($old_text, $new_text, $new_xml);
				}
			}
		}

		return $new_xml;
	}

	
	$xml = $data;
	$xml = simplexml_unCDATAise($xml);
	$objt = simplexml_load_string($xml);

	$xmlarray = array();
	$xmlarray = simpleXMLToArray($objt);
	
	for($j = 0; $j < 4; $j++){
		echo $xmlarray['headword'][$j];
		echo ' ';
	}
	
	
?>
